window.onload = function() {
	var judgeOff = true;
	var oH = $(window).height();

	function oHeight() {
		var oHeight = $(window).height()-60;
		var TwinkleHt=$(window).height()-($(window).height()-30);

		if(oHeight < 700) {
			$(".BlockChainBg").css({
				"height": oHeight
			})
			$(".Twinkle").css({
				"bottom": TwinkleHt
			})
			$(".Twinkle").show();
		} else {
			$(".Twinkle").hide();
		}
	}
	oHeight();

	oChecked()

	function oChecked() {
		var oCheckEl = $(".Checkbox .CheckRadio");
		for(var i = 0; i < oCheckEl.length; i++) {
			oCheckEl.eq(i).attr("index", i);
		}

		$(".Checkbox .CheckRadio").click(function() {
			var _this = $(this);
			var val = $(this).val();

			if(val == null) {
				return false;
			} else {
				var oIndex = $(this).attr("index");
				if(oIndex == 0) {
					$(".searchTxt").attr("placeholder", "请输入区块哈希/高度");
					judgeOff = true;
				} else {
					$(".searchTxt").attr("placeholder", "请输入交易哈希");
					judgeOff = false;
				}

				$(".CheckRadioEl").removeClass("active");
				$(".CheckRadioEl").eq($(this).attr("index")).addClass("active")
			}
		})

	}

	function search() {
		$(".searchBtn").click(function() {
			var searchValue = $(".searchTxt").val();
			var reg = /^[1-9]\d*$|^0$/;

			if(judgeOff) {
				if(reg.test(searchValue) == true) {
					// 查询某一个区块（按照区块高度）  0
					window.location.href='/block.html?QueryType=1&StartIndex='+ searchValue;
					return true;
				} else {
					//查询某一个区块（按照区块Hash）  1
					window.location.href='/block.html?QueryType=0&BlockHash='+ searchValue;
					return false;
				}
			} else {
				 //查询某一次交易信息（按照txid）   2
				window.location.href='/transaction.html?TransactionId='+ searchValue;
			}
		})
	}
	search();
	
	// $(".InstalledNumberBox").click(function(){
	// 	window.location.href='/Intelligence.html';
	// })
	
	// $(".transationNumberBox").click(function(){
	// 	window.location.href='/transaction.html';
	// })
	
	// $(".nodeNumberBox").click(function(){
	// 	window.location.href='/node.html';
	// })
	
	// $(".blockNumberBox").click(function(){
	// 	window.location.href='/block.html';
	// })	
	
	oScroll();

	function oScroll() {
		$(".Twinkle").click(function() {
			scrollPosition("mianChart")
		})
	}

	function getScrollOffsets(_w) { //get the offset position
		_w = _w || window;
		//for all and IE9+
		if(_w.pageXOffset != null) return {
			x: _w.pageXOffset,
			y: _w.pageYOffset
		};
		//for IE678
		var _d = _w.document;
		if(document.compatMode == "CSS1Compat") return { //for IE678
			x: _d.documentElement.scrollLeft,
			y: _d.documentElement.scrollTop
		};
		//for other mode
		return {
			x: _d.body.scrollLeft,
			y: _d.body.scrpllTop
		};
	}

	function getViewPortSize(_w) { //get the window size
		_w = _w || window;
		//for all and IE9+
		if(_w.innerWidth != null) return {
			x: _w.innerWidth,
			y: _w.innerHeight
		};
		//for IE678
		var _d = _w.document;
		if(document.compatMode == "CSS1Compat") return { //for IE678
			x: _d.documentElement.clientWidth,
			y: _d.documentElement.clientHeight
		};
		//for other mode
		return {
			x: _d.body.clientWidth,
			y: _d.body.clientHeight
		};

	}

	function scrollPosition(_obj) {
		var targetX, targetY;
		if(!_obj) {
			targetX = 0;
			targetY = 0;
		} else {
			if(typeof(_obj) == "string") {
				_obj = document.getElementById(_obj);
			} else {
				_obj = _obj
			}
			targetX = _obj.getBoundingClientRect().left + getScrollOffsets().x;
			targetY = (_obj.getBoundingClientRect().top + getScrollOffsets().y) - 60;
		}
		var maxTargetX = document.body.scrollWidth - getViewPortSize().x;
		if(targetX >= maxTargetX) targetX = maxTargetX;
		if(targetX < 0) targetX = 0;
		var maxTargetY = document.body.scrollHeight - getViewPortSize().y;
		if(targetY >= maxTargetY) targetY = maxTargetY;
		if(targetY < 0) targetY = 0;

		console.log(targetX, targetY)

		var tempTimer = setInterval(function() {
			var currentY = getScrollOffsets().y;
			var currentX = getScrollOffsets().x;

			var tempTargetY = currentY - (currentY - targetY) / 10;
			var tempTargetX = currentX - (currentX - targetX) / 10;
			if(Math.abs(tempTargetY - currentY) < 1 && tempTargetY - currentY > 0) {
				tempTargetY++;
			}
			if(Math.abs(tempTargetY - currentY) < 1 && tempTargetY - currentY < 0) {
				tempTargetY--;
			}
			if(Math.abs(tempTargetX - currentX) < 1 && tempTargetX - currentX > 0) {
				tempTargetX++;
			}
			if(Math.abs(tempTargetX - currentX) < 1 && tempTargetX - currentX < 0) {
				tempTargetX--;
			}

			//console.log(currentX, tempTargetX, currentY, tempTargetY);
			//			console.log(tempTargetY);
			window.scrollTo(tempTargetX, tempTargetY);

			if(Math.abs(getScrollOffsets().y - targetY) <= 2 && Math.abs(getScrollOffsets().x - targetX) <= 2) {
				clearInterval(tempTimer);
				window.scrollTo(targetX, targetY);
				//console.log("done");
			}
		}, 10);

	}

	window.onscroll = function() {
		if(getScrollOffsets(window).y >= (oH - 160)) {
			$(".Twinkle").hide();
		} else {
			$(".Twinkle").show();
		}
	}
}